<?php

namespace app\activity\controller;

use app\activity\model\Customer as CustomerModel;
use app\activity\model\Letter as LetterModel;
use app\activity\model\Manager as ManagerModel;
use app\common\controller\Auth;
use app\common\behavior\Cdn;

class Recommend extends Auth
{
    private $letterModel;
    private $customerModel;
    private $managerModel;
    public function __construct(LetterModel $letterModel,CustomerModel $customerModel,ManagerModel $managerModel)
    {
        parent::__construct();
        $this->letterModel = $letterModel;
        $this->customerModel = $customerModel;
        $this->managerModel = $managerModel;
    }


    protected function _pages()
    {
        $page = (int)$this->request->param('page', 1);
        if ($page < 1) {
            $page = 1;
        }
        $limit = (int)$this->request->param('limit', 20);
        if (!$limit) {
            $limit = 20;
        }
        if ($limit > 100) {
            $limit = 100;
        }
        $offset = $limit * ($page - 1);

        return ['offset' => $offset, 'limit' => $limit];
    }

    //确认函列表
    public function letterList(){
        if ($this->request->isAjax()) {
            $page = $this->_pages();
            $list = $this->letterModel->getPageList($page['offset'],$page['limit']);
            $list = $list->toArray();

            foreach($list as $k=>$v){
                $list[$k]['customer_name'] = $this->letterModel->Customer()->where(['id'=>$v['customer_id']])->value('realname');
                $list[$k]['manager_name'] = $this->letterModel->Manager()->where(['id'=>$v['manager_id']])->value('manager_name');
                $list[$k]['sign_url'] = $v['sign_url'] ? Cdn::getCdn($v['sign_url']) : '';
                $list[$k]['status'] = LetterModel::STATUS[$v['status']];
            }

            $total = $this->letterModel->getCount();
            return $this->jsonData($list,$total);
        }
        return $this->fetch('letterList');
    }

    public function test(){
        echo 1;
    }

    //报名列表
    public function signUpList(){
        if ($this->request->isAjax()) {
            $page = $this->_pages();
            $list = $this->customerModel->getPageList($page['offset'],$page['limit'],[['parentid','neq',0]]);
            $list = $list->toArray();
            //print_r($list);exit;

            foreach($list as $k=>$v){
                $customer = $this->customerModel->Customer()->where('id',$v['parentid'])->find(); //推荐客户
                $manager = $this->letterModel->Manager()->where('id',$v['manager_id'])->find();  //客户经理

                $list[$k]['recommend_realname'] = $customer->realname;   //推荐客户姓名
                $list[$k]['recommend_number'] = $customer->customer_number;  //推荐客户客户号
                $list[$k]['manager_name'] = $manager->manager_name; //客户经理姓名
                $list[$k]['manager_number'] = $manager->manager_number; //客户经理员工号
                $list[$k]['manager_bank_outlets'] = $manager->bank_outlets; //客户经理网点

/*                $list[$k]['recommend_realname'] = $this->customerModel->Customer()->where(['id'=>$v['parentid']])->value('realname');
                $list[$k]['manager_name'] = $this->letterModel->Manager()->where(['id'=>$v['manager_id']])->value('manager_name');*/
            }

            $total = $this->customerModel->getCount([['parentid','neq',0]]);
            return $this->jsonData($list,$total);
        }
        return $this->fetch('signUpList');
    }


}